package com.li.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.li.pojo.Message;
import com.li.pojo.ResultMessage;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MessageUtils {
    private static SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd  HH:mm");
    public static String getDate(){
        return f.format(new Date());
    }
    public static String getMessage(String isGroup,String form,Object to,Object message,String event){
        try{
            Message result = new Message();

            result.setIsGroup(isGroup);
            result.setFrom(form);
            result.setTo(to);
            result.setMessage(message);
            result.setDate(f.format(new Date()));
            result.setEvent(event);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(result);

        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }
    public static String getMessage(Message message){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(message);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static Message getMessage(String message){
        ObjectMapper mapper = new ObjectMapper();
        try {
            Message message1 = mapper.readValue(message,Message.class);
                    message1.setDate(MessageUtils.getDate());
            return message1;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
